# -*- coding: utf-8 -*-

import wx
import optionVar

class FindDialog(wx.Dialog):
	def __init__(self, parent, id):
		title = 'Find'
		wx.Dialog.__init__(self, parent, id, title, size=(450, 120))
		panel = wx.Panel(self, -1)
		
		searchWordST = wx.StaticText(self, -1,
									 u"Search Word: ",
									 size=(80, -1)
									 )
		if optionVar.hasOption('SearchHistory'):
			self.searchHistory = optionVar.getListOption('SearchHistory')
		else:
			self.searchHistory = []
		if optionVar.hasOption('SearchWord'):
			searchWordDef = optionVar.getOption('SearchWord')
		else:
			searchWordDef = ''
			
		self.searchWordCB = wx.ComboBox(self, -1, searchWordDef,
								   choices = self.searchHistory,
								   size = (100, -1),
								   style = wx.CB_DROPDOWN
								   )
		searchSizer = wx.BoxSizer(wx.HORIZONTAL)
		space = 8
		searchSizer.Add(searchWordST, 0, wx.ALIGN_RIGHT|wx.RIGHT, space)
		searchSizer.Add(self.searchWordCB, 1, wx.EXPAND|wx.RIGHT, space)
		
		# values
		self.mode = ''
		self.searchWord = ''
		self.option_upperLower = ''
		self.direction = ''
		
		# Option
		if optionVar.hasOption('SearchOption_upperLower'):
			UpperLowerDef = optionVar.getOption('SearchOption_upperLower')
		else:
			UpperLowerDef = ''
		
		OptionSB = wx.StaticBox(self, -1, "Option")
		OptionSizer = wx.StaticBoxSizer(OptionSB, wx.VERTICAL)
		self.UpperLowerCB = wx.CheckBox(self, -1, "Upper/lowercase",)
		if not UpperLowerDef == '':
			self.UpperLowerCB.SetValue(bool(UpperLowerDef))
		OptionSizer.Add(self.UpperLowerCB, 0)
		
		# direction
		if optionVar.hasOption('SearchDirection'):
			directionDef = optionVar.getOption('SearchDirection')
		else:
			directionDef = ''
		directionList = ['Down','Up', ]
		self.directionRB = wx.RadioBox(self, -1, "Direction",
									   choices = directionList,
									   majorDimension = 2,
									   style = wx.RA_SPECIFY_COLS
									   )
		if not directionDef == '':
			self.directionRB.SetStringSelection(directionDef)
		BottomSizer  = wx.BoxSizer(wx.HORIZONTAL)
		BottomSizer.Add(OptionSizer, 1, wx.EXPAND|wx.RIGHT, space)
		BottomSizer.Add(self.directionRB, 1, wx.EXPAND)
		
		leftAllSizer = wx.BoxSizer(wx.VERTICAL)
		leftAllSizer.Add(searchSizer, 0,wx.EXPAND)
		leftAllSizer.Add(BottomSizer, 0)
		
		# Button
		searchFromHeadB = wx.Button(self, -1, "Search From Head",
								   size=(120,25))
		searchNextB = wx.Button(self, -1, "Search Next",
								   size=(120,25))
		searchCancelB = wx.Button(self, -1, "Cancel",
								   size=(120,25))
		self.Bind(wx.EVT_BUTTON, self.OnSearchFromHead, searchFromHeadB)
		self.Bind(wx.EVT_BUTTON, self.OnSearchNext, searchNextB)
		self.Bind(wx.EVT_BUTTON, self.OnSearchCancel, searchCancelB)
		
		buttonsSizer = wx.BoxSizer(wx.VERTICAL)
		buttonsSizer.Add(searchFromHeadB, 0)
		buttonsSizer.Add(searchNextB, 0)
		buttonsSizer.Add(searchCancelB, 0)
		
		mainSizer = wx.BoxSizer(wx.HORIZONTAL)
		mainSizer.Add(leftAllSizer, 1, wx.ALL, space)
		mainSizer.Add(buttonsSizer, 0, wx.TOP|wx.BOTTOM|wx.RIGHT, space)
		
		self.SetSizer(mainSizer)
		
	def OnSearchFromHead(self, event):
		self.mode = 'SearchFromHead'
		self.searchWord = self.searchWordCB.GetValue()
		self.option_upperLower = self.UpperLowerCB.GetValue()
		self.direction = self.directionRB.GetStringSelection()
		self.SaveOptions()
		self.EndModal(wx.ID_OK)
	
	def OnSearchNext(self, event):
		self.mode = 'SearchNext'
		self.searchWord = self.searchWordCB.GetValue()
		self.option_upperLower = self.UpperLowerCB.GetValue()
		self.direction = self.directionRB.GetStringSelection()
		self.SaveOptions()
		self.EndModal(wx.ID_OK)
	
	def OnSearchCancel(self, event):
		self.EndModal(wx.ID_CANCEL)
		
	def SaveOptions(self):
		if self.searchWord != '':
			optionVar.saveOption('SearchWord', self.searchWord)
		if self.searchWord != '' and not self.searchWord in self.searchHistory:
			self.searchHistory.insert(0, self.searchWord)
			optionVar.saveOption('SearchHistory', self.searchHistory)
		optionVar.saveOption('SearchOption_upperLower', self.option_upperLower)
		optionVar.saveOption('SearchDirection', self.direction)
